package com.beardedcoder.lowPower.activities;

import com.beardedcoder.lowPower.R;
import com.beardedcoder.lowPower.service.PowerService;
import com.beardedcoder.utils.ui.ActionBar;
import com.beardedcoder.utils.ui.ActionBar.ActionText;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

public class SettingsActivity extends Activity {
	ActionBar actionbar;
	CheckBox viewLogUnplug;
	Boolean logUnplug = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		loadViews();
	}

	private void loadViews() {
		setContentView(R.layout.settings_activity);
		viewLogUnplug = (CheckBox) findViewById(R.id.settings_activity_checkbox_log_unplug);
		loadActionbar();
	}
	
	@Override
	protected void onPause() {
		if (logUnplug != null) {
			Intent intent = new Intent(this, PowerService.class);
			String action = logUnplug ? PowerService.INTENT_ACTION_LAST_UNPLUG_EVENT_STORE : PowerService.INTENT_ACTION_LAST_UNPLUG_EVENT_IGNORE;
			intent.setAction(action);
			startService(intent);
		}
		super.onPause();
	}

	private void loadActionbar() {
		actionbar = (ActionBar) findViewById(R.id.actionbar);
		ActionText title = new ActionBar.ActionText(this) {
			public void actionClicked() {}
		}; 
		title.setText("Settings Screen");
		title.setTextColour(getResources().getColor(android.R.color.black));
		actionbar.addAction(title, ActionBar.CONTAINER_TITLE);
	}
	
	public void onClickLogUnplug (View view) {
		viewLogUnplug.setChecked(!viewLogUnplug.isChecked());
		logUnplug = viewLogUnplug.isChecked();
	}
	
    /**
     * call through from XML
     * @param view
     */
    public void onClickToastSize(View view) {
    	startServiceWithActionString(PowerService.INTENT_ACTION_TOAST_DATA_SIZE);
    }
    
    public void onClickWipeData (View view) {
    	startServiceWithActionString(PowerService.INTENT_ACTION_DELETE_BATTERY_STORAGE_DATA);
    }

	private void startServiceWithActionString(String action) {
		Intent service = new Intent(getApplicationContext(), PowerService.class);
		service.setAction(action);
		startService(service);
	}

	/**
	 * this will copy the application data to the SDCard. 
	 * for the battery data information
	 * @param view
	 */
	public void onClickMoveToSD(View view) {
		startServiceWithActionString(PowerService.INTENT_ACTION_COPY_DATA_TO_SD);
	}

}
